package com.song.tools.songtest.convertor;

import com.song.tools.core.util.ObjectUtils;
import com.song.tools.log.systemlog.core.LogTypeEnum;
import com.song.tools.mvc.convertor.EntityConvertor;
import com.song.tools.mvc.convertor.QueryConvertor;
import com.song.tools.mvc.convertor.SaveConvertor;
import com.song.tools.songtest.beans.bo.SystemLogBo;
import com.song.tools.songtest.beans.query.SystemLogQuery;
import com.song.tools.songtest.beans.request.SystemLogQueryRequest;
import com.song.tools.songtest.beans.request.SystemLogSaveRequest;
import com.song.tools.songtest.beans.response.SystemLogQueryResponse;
import com.song.tools.songtest.beans.response.SystemLogSaveResponse;
import com.song.tools.songtest.dao.entity.SystemLog;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.ReportingPolicy;

import java.util.List;

/**
 * <p>
 * 系统日志表 转换器
 * </p>
 *
 * @author song tools
 * @since 2024-07-16
 */
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public abstract class SystemLogConvertor implements QueryConvertor<SystemLogBo, SystemLogQuery, SystemLogQueryRequest, SystemLogQueryResponse>,
        EntityConvertor<SystemLog, SystemLogBo>, SaveConvertor<SystemLogBo, SystemLogSaveRequest, SystemLogSaveResponse> {

    @Override
    public abstract SystemLog entityBoToEntity(SystemLogBo systemLogBo);

    @Override
    public abstract SystemLogBo entityToEntityBo(SystemLog systemLog);

    @Override
    public abstract SystemLogQueryResponse entityBoToQueryResponse(SystemLogBo systemLogBo);

    @Override
    public abstract SystemLogQuery queryRequestToQuery(SystemLogQueryRequest queryRequest);

    @Override
    public abstract SystemLogBo saveRequestToEntityBo(SystemLogSaveRequest request);

    @Override
    public abstract SystemLogSaveResponse entityBoToSaveResponse(SystemLogBo systemLogBo);

    @Override
    public abstract List<SystemLog> entityBoToEntity(List<SystemLogBo> entityBo);

    @Override
    public abstract List<SystemLogBo> entityToEntityBo(List<SystemLog> entity);


    @AfterMapping
    public void afterMappingEntityBoToEntity(@MappingTarget SystemLog systemLog, SystemLogBo systemLogBo) {
        // entityBoToEntity() 内部其它代码执行完之后，最后会自动执行 afterMappingEntityBoToEntity()
    }

    @AfterMapping
    public void afterMappingEntityToEntityBo(@MappingTarget SystemLogBo systemLogBo, SystemLog systemLog) {
        // entityToEntityBo() 内部其它代码执行完之后，最后会自动执行 afterMappingEntityToEntityBo()
    }

    @AfterMapping
    public void afterMappingQueryRequestToQuery(@MappingTarget SystemLogQuery systemLogQuery, SystemLogQueryRequest systemLogQueryRequest) {
        // queryRequestToQuery() 内部其它代码执行完之后，最后会自动执行 afterMappingQueryRequestToQuery()
    }

    @AfterMapping
    public void afterMappingEntityBoToQueryResponse(@MappingTarget SystemLogQueryResponse systemLogQueryResponse, SystemLogBo systemLogBo) {
        // entityBoToQueryResponse() 内部其它代码执行完之后，最后会自动执行 afterMappingEntityBoToQueryResponse()
        if (ObjectUtils.isNotEmpty(systemLogQueryResponse.getLogType())) {
            LogTypeEnum logTypeEnum = stringToLogTypeEnum(systemLogQueryResponse.getLogType());
            if (ObjectUtils.isNotEmpty(logTypeEnum)) {
                systemLogQueryResponse.setLogTypeDesc(logTypeEnum.getDesc());
            }
        }
    }

    @AfterMapping
    public void afterMappingEntityBoToSaveResponse(@MappingTarget SystemLogSaveResponse systemLogSaveResponse, SystemLogBo systemLogBo) {
        // entityBoToSaveResponse() 内部其它代码执行完之后，最后会自动执行 afterMappingEntityBoToSaveResponse()
        if (ObjectUtils.isNotEmpty(systemLogSaveResponse.getLogType())) {
            LogTypeEnum logTypeEnum = stringToLogTypeEnum(systemLogSaveResponse.getLogType());
            if (ObjectUtils.isNotEmpty(logTypeEnum)) {
                systemLogSaveResponse.setLogTypeDesc(logTypeEnum.getDesc());
            }
        }
    }

    @AfterMapping
    public void afterMappingSaveRequestToEntityBo(@MappingTarget SystemLogBo systemLogBo, SystemLogSaveRequest systemLogSaveRequest) {
        // saveRequestToEntityBo() 内部其它代码执行完之后，最后会自动执行 afterMappingSaveRequestToEntityBo()
    }

    public String logTypeEnumToString(LogTypeEnum logTypeEnum) {
        if (logTypeEnum == null) {
            return null;
        }
        return logTypeEnum.getCode();
    }

    public LogTypeEnum stringToLogTypeEnum(String logType) {
        if (logType == null) {
            return null;
        }
        return LogTypeEnum.of(logType);
    }
}
